package com.bytebuddytest.fieldandmethod;

import net.bytebuddy.ByteBuddy;
import net.bytebuddy.description.modifier.Visibility;
import net.bytebuddy.dynamic.scaffold.subclass.ConstructorStrategy;
import net.bytebuddy.implementation.MethodCall;
import org.junit.Assert;
import org.junit.Test;

import java.lang.reflect.Constructor;

/**
 * @author yutianhong
 * @version 1.0
 * @since 2023/10/20 17:49
 */
@SuppressWarnings({"resource", "unused"})
public class SpecificMethodCallTest {
    @Test
    public void testDefineConstructor() throws Exception {
        Class<?> dynamicClass = new ByteBuddy()
                .subclass(Object.class, ConstructorStrategy.Default.NO_CONSTRUCTORS)
                .defineConstructor(Visibility.PUBLIC)
                .withParameter(Integer.TYPE)
                .intercept(MethodCall.invoke(Object.class.getDeclaredConstructor()))
                .make()
                .load(ClassLoader.getSystemClassLoader())
                .getLoaded();
        Constructor<?>[] constructors = dynamicClass.getConstructors();
        Assert.assertEquals(constructors.length, 1);
        Constructor<?> constructor = constructors[0];
        Object object = constructor.newInstance(1);
        System.out.println(object);
    }
}
